package com.lxw.library.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;

import com.lxw.library.R;
import com.lxw.library.bean.SpinnerInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * [底部弹出dialog]
 *
 * @author devin.hu
 * @version 1.0
 * @date 2014-1-15
 **/
public class BottomListDialog extends Dialog {

    RecyclerView mRecyclerview;
    RecyclerView.Adapter mAdapter;
    Context mContext;
    BottomListSelectListener listSelectListener;
    List<SpinnerInfo> list = new ArrayList<>();
    /**
     * @param context
     */
    public BottomListDialog(Context context, BottomListSelectListener listSelectListener) {
        super(context, R.style.dialogFullscreen);
        this.listSelectListener = listSelectListener;
    }

    /**
     * @param context
     */
    public BottomListDialog(int type, Context context) {
        super(context, R.style.dialogFullscreen);
    }

    /**
     * @param context
     * @param theme
     */
    public BottomListDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottomlist);
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        layoutParams.dimAmount = 0.5f;
        window.setGravity(Gravity.BOTTOM);
        window.setAttributes(layoutParams);
        window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        initRecyclerView();
    }


    public void initRecyclerView() {
        mRecyclerview = findViewById(R.id.recyclerview);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecyclerview.setLayoutManager(layoutManager);

        mAdapter = new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int position) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_bottomlist, null, false);
                return new RecyclerHolder(view);
            }
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
                RecyclerHolder holder = (RecyclerHolder) viewHolder;
                final SpinnerInfo spinnerInfo = list.get(position);
                holder.setOnItemClick(position,spinnerInfo);
            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        };

        mRecyclerview.setAdapter(mAdapter);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        dismiss();
        return true;
    }


    public void disPlay(List<SpinnerInfo> itemList) {
        show();
        list.clear();
        list.addAll(itemList);
        mAdapter.notifyDataSetChanged();
    }


    public interface BottomListSelectListener {
        void itemClickCallback(int position, SpinnerInfo spinnerInfo);
    }

    class RecyclerHolder extends RecyclerView.ViewHolder {
        public RecyclerHolder( View itemView) {
            super(itemView);
        }

        public void setOnItemClick(final int position,final SpinnerInfo spinnerInfo) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listSelectListener.itemClickCallback(position,spinnerInfo);
                    dismiss();
                }
            });
        }
    }

}
